Page({
  data: {
    isHeaderShowing:true,   //功能栏显示
    speIndex:0,             //规格索引
    goodNum:1,              //商品购买数量
    goodData:[],            //商品数据
    commentText:'',         //输入的评论
    commentList:[]          //商品的评论信息
  },
  //bindTapChangeSpe 更换规格响应函数
  bindTapChangeSpe(e){
    this.setData({
      speIndex:+e.detail.value
    })
  },
  //bindTapAddNum 点击增加商品数量响应函数
  bindTapAddNum(){
    this.setData({
      goodNum:(this.data.goodNum+=1)
    })
  },
  //bindTapSubNum 点击减少商品数量响应函数
  bindTapSubNum(){
    var num = this.data.goodNum-=1;
    this.setData({
      goodNum:(num<=0?1:num)
    })
  },
  //onTextareaInput 评论框输入响应函数
  onTextareaInput(e){
    this.setData({
      commentText:e.detail.value
    });
  },
  //bindTapAddCom 点击发表评论响应函数
  bindTapAddCom(){
    if(this.data.commentText == ''){//检查内容是否为空
      wx.showToast({title: '请输入内容',icon:'none',duration:1000});
      return;
    }    
    var app = getApp();
    var that = this;
    if(app.isLogined() == 0) return;//检查是否登录
    var cid = app.globalData.clientInfo.cid;//获取cid
    var gid = this.data.goodData.gid;
    app.pinia('addComment',{
      cid:cid,
      gid:gid,
      words:that.data.commentText
    },()=>{
      wx.showToast({
        title: '评论成功',
        icon:'success',
        duration:1000
      });
      app.pinia('checkComment',{
        gid:that.data.goodData.gid
      },(res)=>{
        that.setData({commentList:res.data});
      });
    });
    this.setData({commentText:''});
  },
  //bindTapAddShopcart 点击添加到购物车响应函数
  bindTapAddShopcart(){
    var gid = this.data.goodData.gid;
    var app = getApp();
    var that = this;
    //检查是否登录
    if(app.isLogined() == 0) return;
    //检查是否已经添加了
    var isExsit = false;
    app.globalData.shopcart.forEach((item)=>{
      if(item.gid == gid) isExsit = true;
    });
    if(isExsit){
      wx.showToast({
        title: '已存在',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    //添加购物车
    app.pinia('addShopCart',{
      cid:app.globalData.clientInfo.cid,
      gid:gid,
      num:that.data.goodNum,
      priceIndex:that.data.speIndex
    },(res)=>{
      console.log(res.data);
      app.globalData.shopcart=res.data;
      this.setData({shopcartNum:this.data.shopcartNum+=1});
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 1000
      });
    });
  },
  //bindTapShowGal 点击将图画移到画廊中展示响应函数
  bindTapShowGal(){
    (getApp()).globalData.galleryPic = this.data.goodData.smallPicUrl;
    wx.navigateTo({
      url: '/pages/gallery/gallery'
    });
  },
  //forScroll 监听滚动响应函数
  forScroll(e){
    //滚动判断显示底部导航
    if(e.detail.deltaY<0){
      this.setData({
        isHeaderShowing:false
      })
    }else{
      this.setData({
        isHeaderShowing:true
      })
    }
  },
  onLoad(options) {
    var that = this;
    var app = getApp();
    this.setData({themeIndex:(getApp()).globalData.themeIndex});  //设置主题
    const eventChannel = this.getOpenerEventChannel();            //设置参数接收通道
    eventChannel.on('goodData', function(data) {                  //接收goodData并加载对应评论
      that.setData({goodData:data.data[0]});
      app.pinia('checkComment',{
        gid:that.data.goodData.gid
      },(res)=>{
        that.setData({commentList:res.data});
      });
    });
    this.setData({shopcartNum:(getApp()).globalData.shopcart.length});//获取购物车数量
  }
})